home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / dwimg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.1 KB  |  422 lines

  1. /* Copyright (C) 1996, Russell Lang.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. // $Id: dwimg.cpp,v 1.2 2000/09/19 19:00:09 lpd Exp $
  20.  
  21.  
  22. #define STRICT
  23. #include <windows.h>
  24. #include <shellapi.h>
  25. extern "C" {
  26. #include "gsdll.h"
  27. }
  28. #include "dwmain.h"
  29. #include "dwdll.h"
  30. #include "dwimg.h"
  31.  
  32. extern gsdll_class gsdll;
  33.  
  34. char szImgName[] = "Ghostscript Image";
  35. #define M_COPY_CLIP 1
  36. #if defined(_MSC_VER) && defined(__WIN32__)
  37. #define _export
  38. #else
  39.   /* Define  min and max, but make sure to use the identical definition */
  40.   /* to the one that all the compilers seem to have.... */
  41. #  ifndef min
  42. #    define min(a, b) (((a) < (b)) ? (a) : (b))
  43. #  endif
  44. #  ifndef max
  45. #    define max(a, b) (((a) > (b)) ? (a) : (b))
  46. #  endif
  47. #endif
  48.  
  49. // Forward references
  50. LRESULT CALLBACK _export WndImgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  51.  
  52. HINSTANCE ImageWindow::hInstance;
  53. HWND ImageWindow::hwndtext;
  54. ImageWindow *ImageWindow::first;
  55.  
  56.  
  57. // friend
  58. ImageWindow *
  59. FindImageWindow(char FAR *dev)
  60. {
  61.     ImageWindow *iw;
  62.     for (iw = ImageWindow::first; iw!=0; iw=iw->next) {
  63.     if (iw->device == dev)
  64.         return iw;
  65.     }
  66.     return NULL;
  67. }
  68.  
  69.  
  70. // open window for device and add to list
  71. void 
  72. ImageWindow::open(char FAR *dev)
  73. {
  74.     // register class if first window
  75.     if (first == (ImageWindow *)NULL)
  76.     register_class();
  77.    
  78.     // add to list
  79.     if (first)
  80.     next = first;
  81.     first = this;
  82.  
  83.     // remember device handle
  84.     device = dev;
  85.  
  86.     // open window
  87.     create_window();
  88. }
  89.  
  90. // close window and remove from list
  91. void
  92. ImageWindow::close(void)
  93. {
  94.     DestroyWindow(hwnd);
  95.  
  96.     // remove from list
  97.     if (this == first) {
  98.     first = this->next;
  99.     }
  100.     else {
  101.     ImageWindow *tmp;
  102.     for (tmp = first; tmp!=0; tmp=tmp->next) {
  103.         if (this == tmp->next)
  104.         tmp->next = this->next;
  105.     }
  106.     }
  107.     
  108. }
  109.  
  110.  
  111. void
  112. ImageWindow::register_class(void)
  113. {
  114.     WNDCLASS wndclass;
  115.  
  116.     /* register the window class for graphics */
  117.     wndclass.style = CS_HREDRAW | CS_VREDRAW;
  118.     wndclass.lpfnWndProc = WndImgProc;
  119.     wndclass.cbClsExtra = 0;
  120.     wndclass.cbWndExtra = sizeof(LONG);
  121.     wndclass.hInstance = hInstance;
  122.     wndclass.hIcon = LoadIcon(hInstance,(LPSTR)MAKEINTRESOURCE(GSIMAGE_ICON));
  123.     wndclass.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
  124.     wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  125.     wndclass.lpszMenuName = NULL;
  126.     wndclass.lpszClassName = szImgName;
  127.     RegisterClass(&wndclass);
  128. }
  129.  
  130. void
  131. ImageWindow::create_window(void)
  132. {
  133.     HMENU sysmenu;
  134.  
  135.     cxClient = cyClient = 0;
  136.     nVscrollPos = nVscrollMax = 0;
  137.     nHscrollPos = nHscrollMax = 0;
  138.  
  139.     // create window
  140.     hwnd = CreateWindow(szImgName, (LPSTR)szImgName,
  141.           WS_OVERLAPPEDWINDOW,
  142.           CW_USEDEFAULT, CW_USEDEFAULT, 
  143.           CW_USEDEFAULT, CW_USEDEFAULT, 
  144.           NULL, NULL, hInstance, (void FAR *)this);
  145.     ShowWindow(hwnd, SW_SHOWMINNOACTIVE);
  146.  
  147.     // modify the menu to have the new items we want
  148.     sysmenu = GetSystemMenu(hwnd, 0);    // get the sysmenu
  149.     AppendMenu(sysmenu, MF_SEPARATOR, 0, NULL);
  150.     AppendMenu(sysmenu, MF_STRING, M_COPY_CLIP, "Copy to Clip&board");
  151. }
  152.  
  153.     
  154. void
  155. ImageWindow::sync(void)
  156. {
  157.     if ( !IsWindow(hwnd) )    // some clod closed the window
  158.     create_window();
  159.  
  160.     if ( !IsIconic(hwnd) ) {  /* redraw window */
  161.     InvalidateRect(hwnd, NULL, 1);
  162.     UpdateWindow(hwnd);
  163.     }
  164. }
  165.  
  166.  
  167. void
  168. ImageWindow::page(void)
  169. {
  170.     if (IsIconic(hwnd))    // useless as an Icon so fix it
  171.     ShowWindow(hwnd, SW_SHOWNORMAL);
  172.     BringWindowToTop(hwnd);
  173.  
  174.     sync();
  175. }
  176.  
  177.  
  178. void
  179. ImageWindow::size(int x, int y)
  180. {
  181.     width = x;
  182.     height = y;
  183. }
  184.  
  185.  
  186. /* image window */
  187. LRESULT CALLBACK _export
  188. WndImgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  189. {
  190.     ImageWindow *iw;
  191.     if (message == WM_CREATE) {
  192.     // Object is stored in window extra data.
  193.     // Nothing must try to use the object before WM_CREATE
  194.     // initializes it here.
  195.     iw = (ImageWindow *)(((CREATESTRUCT FAR *)lParam)->lpCreateParams);
  196.     SetWindowLong(hwnd, 0, (LONG)iw);
  197.     }
  198.  
  199.     // call the object window procedure
  200.     iw = (ImageWindow *)GetWindowLong(hwnd, 0);
  201.     return iw->WndProc(hwnd, message, wParam, lParam);
  202. }
  203.  
  204. /* graphics window */
  205. LRESULT 
  206. ImageWindow::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  207. {    
  208.     HDC hdc;
  209.     PAINTSTRUCT ps;
  210.     RECT rect;
  211.     int nVscrollInc, nHscrollInc;
  212.  
  213.     switch(message) {
  214.     case WM_SYSCOMMAND:
  215.         // copy to clipboard
  216.         if (LOWORD(wParam) == M_COPY_CLIP) {
  217.         HGLOBAL hglobal;
  218.         HPALETTE hpalette;
  219.         gsdll.lock_device(device, 1);
  220.         hglobal = gsdll.copy_dib(device);
  221.         if (hglobal == (HGLOBAL)NULL) {
  222.             MessageBox(hwnd, "Not enough memory to Copy to Clipboard", 
  223.             szImgName, MB_OK | MB_ICONEXCLAMATION);
  224.             gsdll.lock_device(device, 0);
  225.             return 0;
  226.         }
  227.         OpenClipboard(hwnd);
  228.         EmptyClipboard();
  229.         SetClipboardData(CF_DIB, hglobal);
  230.         hpalette = gsdll.copy_palette(device);
  231.         if (hpalette)
  232.             SetClipboardData(CF_PALETTE, hpalette);
  233.         CloseClipboard();
  234.         gsdll.lock_device(device, 0);
  235.         return 0;
  236.         }
  237.         break;
  238.     case WM_CREATE:
  239.         // enable drag-drop
  240.         DragAcceptFiles(hwnd, TRUE);
  241.         break;
  242.     case WM_SIZE:
  243.         if (wParam == SIZE_MINIMIZED)
  244.             return(0);
  245.         cyClient = HIWORD(lParam);
  246.         cxClient = LOWORD(lParam);
  247.  
  248.         cyAdjust = min(height, cyClient) - cyClient;
  249.         cyClient += cyAdjust;
  250.  
  251.         nVscrollMax = max(0, height - cyClient);
  252.         nVscrollPos = min(nVscrollPos, nVscrollMax);
  253.  
  254.         SetScrollRange(hwnd, SB_VERT, 0, nVscrollMax, FALSE);
  255.         SetScrollPos(hwnd, SB_VERT, nVscrollPos, TRUE);
  256.  
  257.         cxAdjust = min(width,  cxClient) - cxClient;
  258.         cxClient += cxAdjust;
  259.  
  260.         nHscrollMax = max(0, width - cxClient);
  261.         nHscrollPos = min(nHscrollPos, nHscrollMax);
  262.  
  263.         SetScrollRange(hwnd, SB_HORZ, 0, nHscrollMax, FALSE);
  264.         SetScrollPos(hwnd, SB_HORZ, nHscrollPos, TRUE);
  265.  
  266.         if ((wParam==SIZENORMAL) && (cxAdjust!=0 || cyAdjust!=0)) {
  267.         GetWindowRect(GetParent(hwnd),&rect);
  268.         MoveWindow(GetParent(hwnd),rect.left,rect.top,
  269.             rect.right-rect.left+cxAdjust,
  270.             rect.bottom-rect.top+cyAdjust, TRUE);
  271.         cxAdjust = cyAdjust = 0;
  272.         }
  273.         return(0);
  274.     case WM_VSCROLL:
  275.         switch(LOWORD(wParam)) {
  276.         case SB_TOP:
  277.             nVscrollInc = -nVscrollPos;
  278.             break;
  279.         case SB_BOTTOM:
  280.             nVscrollInc = nVscrollMax - nVscrollPos;
  281.             break;
  282.         case SB_LINEUP:
  283.             nVscrollInc = -cyClient/16;
  284.             break;
  285.         case SB_LINEDOWN:
  286.             nVscrollInc = cyClient/16;
  287.             break;
  288.         case SB_PAGEUP:
  289.             nVscrollInc = min(-1,-cyClient);
  290.             break;
  291.         case SB_PAGEDOWN:
  292.             nVscrollInc = max(1,cyClient);
  293.             break;
  294.         case SB_THUMBTRACK:
  295.         case SB_THUMBPOSITION:
  296. #ifdef __WIN32__
  297.             nVscrollInc = HIWORD(wParam) - nVscrollPos;
  298. #else
  299.             nVscrollInc = LOWORD(lParam) - nVscrollPos;
  300. #endif
  301.             break;
  302.         default:
  303.             nVscrollInc = 0;
  304.         }
  305.         if ((nVscrollInc = max(-nVscrollPos, 
  306.         min(nVscrollInc, nVscrollMax - nVscrollPos)))!=0) {
  307.         nVscrollPos += nVscrollInc;
  308.         ScrollWindow(hwnd,0,-nVscrollInc,NULL,NULL);
  309.         SetScrollPos(hwnd,SB_VERT,nVscrollPos,TRUE);
  310.         UpdateWindow(hwnd);
  311.         }
  312.         return(0);
  313.     case WM_HSCROLL:
  314.         switch(LOWORD(wParam)) {
  315.         case SB_LINEUP:
  316.             nHscrollInc = -cxClient/16;
  317.             break;
  318.         case SB_LINEDOWN:
  319.             nHscrollInc = cyClient/16;
  320.             break;
  321.         case SB_PAGEUP:
  322.             nHscrollInc = min(-1,-cxClient);
  323.             break;
  324.         case SB_PAGEDOWN:
  325.             nHscrollInc = max(1,cxClient);
  326.             break;
  327.         case SB_THUMBTRACK:
  328.         case SB_THUMBPOSITION:
  329. #ifdef __WIN32__
  330.             nHscrollInc = HIWORD(wParam) - nHscrollPos;
  331. #else
  332.             nHscrollInc = LOWORD(lParam) - nHscrollPos;
  333. #endif
  334.             break;
  335.         default:
  336.             nHscrollInc = 0;
  337.         }
  338.         if ((nHscrollInc = max(-nHscrollPos, 
  339.         min(nHscrollInc, nHscrollMax - nHscrollPos)))!=0) {
  340.         nHscrollPos += nHscrollInc;
  341.         ScrollWindow(hwnd,-nHscrollInc,0,NULL,NULL);
  342.         SetScrollPos(hwnd,SB_HORZ,nHscrollPos,TRUE);
  343.         UpdateWindow(hwnd);
  344.         }
  345.         return(0);
  346.     case WM_KEYDOWN:
  347.         switch(LOWORD(wParam)) {
  348.         case VK_HOME:
  349.             SendMessage(hwnd,WM_VSCROLL,SB_TOP,0L);
  350.             break;
  351.         case VK_END:
  352.             SendMessage(hwnd,WM_VSCROLL,SB_BOTTOM,0L);
  353.             break;
  354.         case VK_PRIOR:
  355.             SendMessage(hwnd,WM_VSCROLL,SB_PAGEUP,0L);
  356.             break;
  357.         case VK_NEXT:
  358.             SendMessage(hwnd,WM_VSCROLL,SB_PAGEDOWN,0L);
  359.             break;
  360.         case VK_UP:
  361.             SendMessage(hwnd,WM_VSCROLL,SB_LINEUP,0L);
  362.             break;
  363.         case VK_DOWN:
  364.             SendMessage(hwnd,WM_VSCROLL,SB_LINEDOWN,0L);
  365.             break;
  366.         case VK_LEFT:
  367.             SendMessage(hwnd,WM_HSCROLL,SB_PAGEUP,0L);
  368.             break;
  369.         case VK_RIGHT:
  370.             SendMessage(hwnd,WM_HSCROLL,SB_PAGEDOWN,0L);
  371.             break;
  372.         case VK_RETURN:
  373.             BringWindowToTop(hwndtext);
  374.             break;
  375.         }
  376.         return(0);
  377.     case WM_CHAR:
  378.         // send on all characters to text window
  379.         if (hwndtext)
  380.         SendMessage(hwndtext, message, wParam, lParam);
  381.         return 0;
  382.     case WM_PAINT:
  383.         {
  384.         int sx,sy,wx,wy,dx,dy;
  385.         hdc = BeginPaint(hwnd, &ps);
  386.         SetMapMode(hdc, MM_TEXT);
  387.         SetBkMode(hdc,OPAQUE);
  388.         gsdll.lock_device(device, 1);
  389.         rect = ps.rcPaint;
  390.         dx = rect.left;    /* destination */
  391.         dy = rect.top;
  392.         wx = rect.right-rect.left; /* width */
  393.         wy = rect.bottom-rect.top;
  394.         sx = rect.left;    /* source */
  395.         sy = rect.top;
  396.         sx += nHscrollPos; /* scrollbars */
  397.         sy += nVscrollPos;    
  398.         if (sx+wx > width)
  399.             wx = width - sx;
  400.         if (sy+wy > height)
  401.             wy = height - sy;
  402.  
  403.         gsdll.draw(device, hdc, dx, dy, wx, wy, sx, sy);
  404.         gsdll.lock_device(device, 0);
  405.  
  406.         EndPaint(hwnd, &ps);
  407.         return 0;
  408.         }
  409.     case WM_DROPFILES:
  410.         SendMessage(hwndtext, message, wParam, lParam);
  411.         break;
  412.     case WM_DESTROY:
  413.         DragAcceptFiles(hwnd, FALSE);
  414.         break;
  415.  
  416.     }
  417.  
  418. not_ours:
  419.     return DefWindowProc(hwnd, message, wParam, lParam);
  420. }
  421.  
  422.